<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<script src="../vue.js"></script>

<body>
  <div id="app">
    <!-- 父组件 -->
    <my-parent></my-parent>
  </div>


  <!-- 父组件模板 -->
  <template id="parent-template">
    <div>
      <h3>手机信息搜索</h3>
      手机品牌：<input type="text" v-model="brand" placeholder="请输入手机品牌">
      <!-- 子组件 -->
      <!-- <my-child v-bind:name="brand"></my-child> -->
      <my-child :name="brand"></my-child>
    </div>
  </template>

  <!-- 子组件模板 -->
  <template id="child-template">
    <ul>
      <li>手机品牌:{{ show.brand }}</li>
      <li>手机型号:{{ show.type }}</li>
      <li>市场价格:{{ show.price }}</li>
    </ul>
  </template>
  <script>
    // 注册父组件
    Vue.component("my-parent", {
      template: "#parent-template",
      data() {
        return {
          brand: ""
        }
      }
    })

    // 注册子组件
    Vue.component("my-child", {
      template: "#child-template",
      data() {
        return {
          // 数组
          content: [{
              brand: '华为',
              type: 'Mate70',
              price: 5999
            },
            {
              brand: '苹果',
              type: 'iPhone17',
              price: 4499
            },
            {
              brand: '小米',
              type: 'xiaomi15Ultra',
              price: 6999
            }
          ],
          // 手机对象
          show: {
            brand: "",
            type: "",
            price: ""
          },
        };
      },
      props: ["name"],
      watch: {
        name() {
          if (this.$props.name) {
            var found = false;
            this.content.forEach((item, index) => {
              if (item.brand === this.$props.name) {
                found = item;
              }
            });
            this.show = found ? found : {
              brand: "",
              type: "",
              price: ""
            }
          } else {
            return
          }
        }
      },
    })
    var vm = new Vue({
      el: "#app",
      data: {}
    });
  </script>
</body>

</html>